package com.leyou.web.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.web.service.IPageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.web.service.impl
 * @description:
 * @author: pc
 * @date: 2019/7/4 20:21
 */
@Service("pageService")
@Slf4j
public class PageServiceImpl implements IPageService {

    @Autowired
    private ItemClient itemClient;
    @Override
    public Map<String, Object> loadItemData(Long spuId) {

        // 查询spu
        SpuDTO spu = itemClient.querySpuById(spuId);
        // 查询分类集合
        List<CategoryDTO> categories = itemClient.queryCategoryByIds(spu.getCategoryIds());
        // 查询品牌
        BrandDTO brand = itemClient.queryBrandById(spu.getBrandId());
        // 查询规格
        List<SpecGroupDTO> specs = itemClient.querySpecsById(spu.getCid3());
        // 封装数据
        Map<String,Object> map = new HashMap<>();

        map.put("categories",categories);
        map.put("brand",brand);
        map.put("spuName",spu.getName());
        map.put("subTitle",spu.getSubTitle());
        map.put("detail",spu.getSpuDetail());
        map.put("skus",spu.getSkus());
        map.put("specs",specs);

        return map;
    }

    private static final String HTML_DIR = "D:\\javaclass\\development\\nginx-1.12.2\\nginx-1.12.2\\html\\item";
    @Autowired
    private SpringTemplateEngine templateEngine;

    @Override
    public void creatItemHtml(Long spuId) {
        Context context = new Context();
        context.setVariables(loadItemData(spuId));
        File file = new File(HTML_DIR, spuId + ".html");
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(file, "UTF-8");
            templateEngine.process("item",context,writer);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【静态页服务】静态页生成失败，商品id：{}", spuId, e);
            throw new LyException(ExceptionEnum.FILE_WRITER_ERROR);
        }
    }

    @Override
    public void deleteItemHtmL(Long spuId) {
        File file = new File(HTML_DIR, spuId + ".html");

        if (file.exists()) {
            boolean result = file.delete();
            System.out.println(result);
            if (!result) {
                throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
            }
        }
    }
}
